import { NodeStatus } from "../NodeStatus";
import { INode } from "../interfaces/INode";
import { SyncActionNode } from "./SyncActionNode";



export class SimpleActionNode<T> extends SyncActionNode<T> {

    protected tickFunctor: (node: INode<T>) => NodeStatus;

    constructor(name: string, blackboard: T, tickFunctor: (node: INode<T>) => NodeStatus) {
        super(name, blackboard);
        this.tickFunctor = tickFunctor;
    }

    Tick(): NodeStatus {
        let prevStatuc = this.status;
        if (prevStatuc === NodeStatus.IDLE) {
            this.status = NodeStatus.RUNNING;
            prevStatuc = NodeStatus.RUNNING;
        }
        let status = this.tickFunctor(this);
        if (status !== prevStatuc) {
            this.status = status;
        }
        return status;
    }
}